/*
 *  Copyright 2013 HCIWriter
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.hci.writer.client.activity;

import java.util.ArrayList;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.hci.writer.client.ClientFactory;
import com.hci.writer.client.StorageService;
import com.hci.writer.client.StorageServiceAsync;
import com.hci.writer.client.place.AdminPlace;
import com.hci.writer.client.ui.AdminView;
import com.hci.writer.shared.Participant;
import com.hci.writer.shared.Task;

public class AdminActivity extends AbstractActivity implements AdminView.Presenter {

	private ClientFactory clientFactory;
	private AdminView adminView;
	private String key;
	private Participant participant;
	
	private final StorageServiceAsync storageService = GWT.create(StorageService.class);
	
	public AdminActivity(AdminPlace place, ClientFactory clientFactory) {
        this.clientFactory = clientFactory;
    }
	
	@Override
	public void start(AcceptsOneWidget containerWidget, EventBus eventBus) {
		this.adminView = clientFactory.getAdminView();
		this.adminView.setPresenter(this);
        containerWidget.setWidget(this.adminView.asWidget());
        
        loadParticipantlist();
	}

	@Override
	public void goTo(Place place) {
		clientFactory.getPlaceController().goTo(place);
	}
	
	@Override
	public void loadParticipantlist() {
		this.adminView.showProgress("Recorded participants");
		
		storageService.getStoredParticipants(new AsyncCallback<ArrayList<String>>() {
			@Override
			public void onSuccess(ArrayList<String> result) {
				adminView.showParticipants(result);
			}
			
			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Fehler beim Übertragen der Daten. Es wird erneut probiert.");
				loadParticipantlist();
			}
		});
	}
	
	@Override
	public void loadTaskList() {
		loadTaskList(this.key);
	}
	
	@Override
	public void loadTaskList(String key) {
		this.key = key;
		this.adminView.showProgress("Solved tasks : " + this.key);
		
		storageService.getParticipant(key, new AsyncCallback<Participant>() {
			@Override
			public void onSuccess(Participant result) {
				AdminActivity.this.participant = result;
				AdminActivity.this.adminView.showTasks(result);
			}
			
			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Fehler beim Übertragen der Daten. Es wird erneut probiert.");
				loadTaskList(AdminActivity.this.key);
			}

		});
	}
	
	@Override
	public void loadTask(String taskName) {
		for(int i = 0; i < participant.getNumberOfTasks(); ++i) {
			Task task = participant.getTask(i);

			if(taskName.equals(task.getTaskName())) {
				this.adminView.replayTask(task);
			}
		}
	}

}
